from django.conf import settings
from django.contrib.auth.models import SiteProfileNotAvailable
from django.db.models import get_model

from userena.utils import generate_sha1, get_datetime_now, \
                            get_user_model, user_model_label

PROFILE_AVATAR_PATH = getattr(settings,
                                'PROFILE_AVATAR_PATH',
                                'avatar/')

def upload_to_avatar(instance, filename):
    """
    Uploads a mugshot for a user to the ``PROFILE_AVATAR_PATH`` and saving it
    under unique hash for the image. This is for privacy reasons so others
    can't just browse through the mugshot directory.

    """
    extension = filename.split('.')[-1].lower()
    salt, hash = generate_sha1(instance.id)
    path = PROFILE_AVATAR_PATH % {'username': instance.user.username,
                                                    'id': instance.user.id,
                                                    'date': instance.user.date_joined,
                                                    'date_now': get_datetime_now().date()}
    return '%(path)s%(hash)s.%(extension)s' % {'path': path,
                                               'hash': hash[:10],
                                               'extension': extension}